//
//  UpdateList.swift
//  DesignCode
//
//  Created by Mithun x on 7/14/19.
//  Copyright © 2019 Mithun. All rights reserved.
//

import SwiftUI

struct ApplyList: View {

   var applications = applyData
   @ObservedObject var store = ApplyStore(updates: applyData)

   func move(from source: IndexSet, to destination: Int) {
      store.applys.swapAt(source.first!, destination)
   }

   var body: some View {
      NavigationView {
         List {
            ForEach(store.applys) { item in
                NavigationLink(destination: ApplyDetail(image: item.image, applyId: item.applyId, date: item.date, type: item.type, reason: item.reason, personName: item.personName, personId: item.personId, status: item.status)) {
                  HStack(spacing: 12.0) {
                     Image(item.image)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 80, height: 80)
                        .background(Color("background"))
                        .cornerRadius(20)

                     VStack(alignment: .leading) {
                        Text(item.applyId)
                           .font(.headline)

                        Text(item.type)
                           .lineLimit(2)
                           .lineSpacing(4)
                           .font(.subheadline)
                           .frame(height: 50.0)

                        Text(item.date)
                           .font(.caption)
                           .fontWeight(.bold)
                           .foregroundColor(.gray)
                     }
                  }
               }
               .padding(.vertical, 8.0)
            }
            .onDelete { index in
               self.store.applys.remove(at: index.first!)
            }
            .onMove(perform: move)
         }
         .navigationBarTitle(Text("申请列表"))
         .navigationBarItems(
//            leading: Button(action: addApply) { Text("Add Update")       },
            trailing: EditButton()
         )
      }
   }
}

#if DEBUG
struct ApplyList_Previews: PreviewProvider {
   static var previews: some View {
      ApplyList()
   }
}
#endif

struct Apply: Identifiable {
   var id = UUID()
   var image: String
//   var title: String
//   var text: String
//   var date: String
    var applyId = ""
    var date = ""
    var type = ""
    var reason = ""
    var personName = ""
    var personId = ""
    var status = ""
}

let applyData = [
    Apply(image: "Illustration1", applyId: "1", date: "JUN 26 - 28", type: "headache", reason: "I Don't Want to Study!!", personName: "LC", personId: "12", status: "permitted"),
    Apply(image: "Illustration2", applyId: "2", date: "JUN 26 - 28", type: "headache", reason: "I Don't Want to Study!!", personName: "LC", personId: "12", status: "permitted"),
    Apply(image: "Illustration3", applyId: "3", date: "JUN 26 - 28", type: "headache", reason: "I Don't Want to Study!!", personName: "LC", personId: "12", status: "permitted"),
    Apply(image: "Illustration4", applyId: "4", date: "JUN 26 - 28", type: "headache", reason: "I Don't Want to Study!!", personName: "LC", personId: "12", status: "permitted"),
    Apply(image: "Illustration5", applyId: "5", date: "JUN 26 - 28", type: "headache", reason: "I Don't Want to Study!!", personName: "LC", personId: "12", status: "permitted"),
    Apply(image: "Illustration6", applyId: "6", date: "JUN 26 - 28", type: "headache", reason: "I Don't Want to Study!!", personName: "LC", personId: "12", status: "permitted"),
    Apply(image: "Illustration7", applyId: "7", date: "JUN 26 - 28", type: "headache", reason: "I Don't Want to Study!!", personName: "LC", personId: "12", status: "permitted")

]
